<?php
class Outgoingincoming_model extends CRM_Model{
	public function AddFinOper($p_docRid, $p_dateOper, $p_stateCod, $p_summValue, $p_currencyRid, $p_filial = null, $p_operType = 'P', $p_operDescr=''){
		$query = $this->db->getwhere('_account_states', array('code'=>$p_stateCod));
		$insArr = array('_documents_rid'=>$p_docRid,
						'_currencies_rid'=>$p_currencyRid,
						'_account_states_rid'=>$query->row()->rid,
						'summ_value'=>$p_summValue,
						'oper_date'=>$p_dateOper,
						'oper_type'=>$p_operType,
						'descr'=>$p_operDescr,
						'modifier_users_rid'=>$this->userRid,
						'modifyDT'=>$this->nowTime);
		$p_filial = trim($p_filial);
		if(!empty($p_filial)) $insArr['_filials_rid'] = $p_filial;		
		$this->db->insert('_outgoing_incoming', $insArr);
	}
	
	public function DelFinOper($p_docRid, $p_stateCode = null){
		if(!$p_stateCode) $this->db->delete('_outgoing_incoming', array('_documents_rid'=>$p_docRid));
		else {
			$query = $this->db->getwhere('_account_states', array('code'=>$p_stateCode));
			if($query->num_rows()) $this->db->delete('_outgoing_incoming', array('_documents_rid'=>$p_docRid, '_account_states_rid'=>$query->row()->rid));
		}
	}
	
	public function GetDocFinOper($p_docRid, $p_stateCode = null){
		if(!$p_stateCode){
			$query = $this->db->getwhere('_outgoing_incoming', array('_documents_rid'=>$p_docRid));
			if($query->num_rows()) return $query->result();
			else return False;
		}
		$query = $this->db->getwhere('_account_states', array('code'=>$p_stateCode));
		if($query->num_rows()){ 
			$query1 = $this->db->getwhere('_outgoing_incoming', array('_documents_rid'=>$p_docRid, '_account_states_rid'=>$query->row()->rid));
			if($query1->num_rows()) return $query1->result();
			else return False; 
		}
		return False;
	}
	
}
?>